package org.gary.admin.action;

import static lengreen.util.constant.Constant.DateFomaterString.YYYY_MM_DD_HH_MM_SS;
import static org.gary.core.util.Utils.makeDateFormateJsonConfig;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.gary.admin.model.Logger;
import org.gary.admin.service.LoggerService;
import org.gary.core.util.Utils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author lengreen
 *
 */
@Component
@Scope("prototype")
public class LoggerAction extends ActionSupport implements ServletResponseAware {
	private static final long serialVersionUID = 2128501959141553917L;

	private HttpServletResponse response;

	private LoggerService loggerService;
	private Logger logger;
	private List<Logger> loggerList;
	/**分页开始页*/
	private int start;
	/**分页每页最大记录数*/
	private int limit;
	/**查询开始时间*/
	private String fromDate;
	/**查询结束时间*/
	private String toDate;

	/**
	 * 列出所有分页中Logger
	 * @throws IOException
	 */
	public void list() throws IOException {
		Object[] orderBy = new Object[] { " entity.id desc " };

		JSONArray loggerListJsonArray = JSONArray.fromObject(loggerService
				.list(Logger.class, start, limit, orderBy),
				makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS));//json格式化时间过滤器
		JSONObject loggerJsonObject = new JSONObject();
		loggerJsonObject.put("loggerTotal", loggerService
				.selectTotalCount(null));
		loggerJsonObject.put("loggerList", loggerListJsonArray);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(loggerJsonObject.toString());
	}

	/**
	 * 搜索
	 * @throws Exception
	 */
	public void search() throws Exception {
		Map<String, Object> where = new HashMap<String, Object>();
		System.out.println("fromDate-->" + getFromDate());
		System.out.println("toDate-->" + getToDate());
		if (!"".equals(getFromDate().trim())) {
			if ("".equals(getToDate().trim())) {
				where.put(" entity.createDate between ", getFromDate()
						+ " 00:00:00' and '"
						+ Utils.getFormatDate(new Date(), YYYY_MM_DD_HH_MM_SS));
			} else {
				where.put(" entity.createDate between ", getFromDate()
						+ " 00:00:00' and '" + getToDate() + " 23:59:59");
			}
		}
		setLoggerList(loggerService.list(Logger.class, getStart(), getLimit(), where,
				new Object[] { " entity.id desc " }));

		JSONArray loggerListJsonArray = JSONArray.fromObject(loggerList, Utils
				.makeDateFormateJsonConfig("yyyy-MM-dd HH:mm:ss"));
		JSONObject loggerJsonObject = new JSONObject();
		loggerJsonObject.put("loggerTotal", loggerService.selectTotalCount(null));
		loggerJsonObject.put("loggerList", loggerListJsonArray);
		System.out.println("loggerJsonObject-->" + loggerJsonObject.toString());
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(loggerJsonObject.toString());
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public Logger getLogger() {
		return logger;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getStart() {
		return start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	@Resource
	public void setLoggerService(LoggerService loggerService) {
		this.loggerService = loggerService;
	}

	public LoggerService getLoggerService() {
		return loggerService;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setLoggerList(List<Logger> loggerList) {
		this.loggerList = loggerList;
	}

	public List<Logger> getLoggerList() {
		return loggerList;
	}
}
